﻿using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using dpa.Library.Models;
using dpa.Library.Services;

namespace dpa.Library.ViewModels;

public partial class TodayViewModel : ViewModelBase
{
    private readonly ITodayPoetryService _todayPoetryService;

    private readonly ITodayImageService _todayImageService;

    private readonly IContentNavigationService _contentNavigationService;

    public TodayViewModel(ITodayImageService todayImageService,
        ITodayPoetryService todayPoetryService,
        IContentNavigationService contentNavigationService)
    {
        _todayImageService = todayImageService;
        _todayPoetryService = todayPoetryService;
        _contentNavigationService = contentNavigationService;

        OnInitializedCommand = new RelayCommand(OnInitializedAsync);
    }

    [ObservableProperty] private TodayPoetry _todayPoetry;

    [ObservableProperty] private bool _isLoading;

    [ObservableProperty] private TodayImage _todayImage;

    public ICommand OnInitializedCommand { get; }

    private void OnInitializedAsync()
    {
        Task.Run(async () =>
        {
            TodayImage = await _todayImageService.GetTodayImageAsync();

            var updateResult = await _todayImageService.CheckUpdateAsync();
            if (updateResult.HasUpdate)
            {
                TodayImage = updateResult.TodayImage;
            }
        });

        Task.Run(async () =>
        {
            IsLoading = true;
            await Task.Delay(1000);
            TodayPoetry = await _todayPoetryService.GetTodayPoetryAsync();
            IsLoading = false;
        });
    }

    [RelayCommand]
    private void ShowDetail()
    {
        _contentNavigationService.NavigateTo(ContentNavigationConstant.TodayDetailView, TodayPoetry);
    }
}